package kxfang.com.common.core.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import kxfang.com.base.activity.MvvmBaseActivity;
import kxfang.com.base.viewmodel.IMvvmBaseViewModel;
import kxfang.com.common.R;
import kxfang.com.common.core.annotation.HasToolBar;
import kxfang.com.common.core.annotation.TitleName;
import kxfang.com.common.core.util.InputUtil;
import kxfang.com.common.core.viewModel.BaseViewModel;

/**
 * 作为承载fragment兼跳转的activity
 * 可以设置actionbar
 */
public class NavigationActivity extends MvvmBaseActivity {

    private Navigate navigate;

    private Class<?> viewClass;// 视图

    public Navigate getNavigate() {
        return navigate;
    }

    private boolean hasBar = true;// 是否包含toolbar

    private Fragment fragment;

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_navigate_view;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    protected void init() {
        Intent intent = getIntent();
        String tag = intent.getAction();
        navigate = Navigate.getInstance(tag);
        navigate.activity = this;
        if (intent.hasExtra("viewCls")) {
            String viewCls = intent.getStringExtra("viewCls");
            try {
                viewClass = Class.forName(viewCls);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        // 设置fragment内容
        if (viewClass != null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            try {
                // 加载fragment
                fragment = (Fragment) viewClass.newInstance();
                transaction.replace(R.id.activity_navigate_content_view, fragment);
                transaction.commit();

                hasBar = getToolBar(fragment);
                if (hasBar) {
                    // 添加toolbar
                    ViewStub viewStub = findViewById(R.id.activity_navigate_toolbar_view);
                    FrameLayout toolBar = (FrameLayout) viewStub.inflate();
                    NavigationBar bar = new NavigationBar(this);
                    navigate.bar = bar;
                    // 返回按钮
                    bar.setLeftButtonOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onBack();
                        }
                    });
                    toolBar.addView(bar);
                    // 设置界面标题
                    setBarTitle(fragment, bar);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 通过注解获取界面是否有无toolbar，没有注解默认有toolBar
     *
     * @param obj
     * @return
     */
    private boolean getToolBar(Object obj) {
        HasToolBar toolBar = obj.getClass().getAnnotation(HasToolBar.class);
        if (toolBar == null || toolBar.hasBar()) {
            return true;
        }
        return false;
    }

    /**
     * 通过注解设置标题
     *
     * @param obj 界面对象
     * @param bar
     */
    private void setBarTitle(Object obj, NavigationBar bar) {
        TitleName titleName = obj.getClass().getAnnotation(TitleName.class);
        if (titleName != null && !TextUtils.isEmpty(titleName.value())) {
            bar.setTitle(titleName.value());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        InputUtil.forceHideSoftKeyboard(NavigationActivity.this);
    }

    @Override
    protected IMvvmBaseViewModel getViewModel() {
        return null;
    }

    /**
     * 返回方法
     */
    public void onSupportBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void onBack() {
        // 判断有无自定义的返回方法
        if (navigate.getListener() == null) {
            InputUtil.forceHideSoftKeyboard(NavigationActivity.this);
            NavigationActivity.super.onBackPressed();
        } else {
            navigate.getListener().onClick();
        }
    }
}
